package com.three.system.controller;


import com.three.core.entity.User;
import com.three.system.service.UserService;
import com.three.core.vo.R;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/system/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping("/getByName")
    public R<User> getByName(@RequestParam String userName) {
        return userService.getByName(userName);
    }
    //退出登录
    @GetMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        String token = request.getHeader("token");
        redisTemplate.delete("user::login::" + token);
        return new R<>(200, "退出成功", null);
    }
    //获取个人信息
    @GetMapping("/info")
    public R<User> getInfo(HttpServletRequest request) {
        String token = request.getHeader("token");
        R<User> r = userService.getByName((String) redisTemplate.opsForHash().get("user::login::" + token, "name"));
        return r;
    }

    @GetMapping("/selectAll")
    public R<List<User>> selectAll() {
        return userService.selectAll();
    }
}
